﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows;

namespace Kokomo.Controls
{
	public class TypedDataGrid : DataGrid
	{
		static TypedDataGrid()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(TypedDataGrid), new FrameworkPropertyMetadata(typeof(TypedDataGrid)));
		}

		protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
		{
			DataGridBoundColumn boundColumn = e.Column as DataGridBoundColumn;
			PropertyDescriptor property = e.PropertyDescriptor as PropertyDescriptor;

			if (
				(boundColumn != null)
				&& (property != null)
				)
			{
				if (!property.IsBrowsable)
				{
					e.Cancel = true;
				}
				else
				{
					boundColumn.Header = property.DisplayName;
					if (property.Converter != null)
					{
						Binding binding = boundColumn.Binding as Binding;
						if (binding != null)
						{
							binding.Converter = new PropertyValueConverter(property.Converter);
						}
					}
				}
			}

			base.OnAutoGeneratingColumn(e);
		}
	}
}
